package com.hshit.downloader.searchengine;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hshit.downloader.models.TrackInfo;
import com.hshit.downloader.utils.Constants;

public class SkullJsoupParser implements JsoupParser {

	@Override
	public List<TrackInfo> getSearchResults(Document doc) {
		//Logger.d(">>>: " + doc.toString());
		Elements results = doc.select(Constants.TAG_DIV + "#" + Constants.ID_SONG_HTML);
		if (results == null || results.size() == 0) return null;
    	List<TrackInfo> tracks = new ArrayList<TrackInfo>();
    	int count = results.size() < 26 ? results.size() : 25;
    	for (int i=0; i< count; i++){
    		Element song_html = results.get(i);
    		TrackInfo track = new TrackInfo();
    		track.setTrack_number(i + "");
    		String track_info = song_html.child(1).child(0).child(0).text();
    		String[] splits = track_info.split("\\-", 2);
    		if(splits.length >= 2) {
    			track.setSinger(splits[0]);
    			String name = splits[1].trim().toString();
    			int index = name.lastIndexOf("mp3");
    			if(index == (name.length() - 3))
    				name = name.substring(0, index).trim();
    			track.setTrack_name(name);
    		} else {
    			track.setSinger("Unknown");
    			track.setTrack_name(track_info);
    		}
    		track.setTrack_link(song_html.child(1).child(2).child(0).child(0).child(0).attr(Constants.ATTR_HREF));
    		track.setFile_type("mp3");
    		//String info = song_html.child(0).text();
    		
    		tracks.add(track);
    	}
		return tracks;
	}

	@Override
	public String getDownloadUrl(Document doc) {
		// TODO Auto-generated method stub
		return null;
	}

}
